package org.example.cloudprovidercontroller8002.controller;

import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import org.example.cloudprovidercontroller8002.service.PaymentFeign;
import org.example.cloudprovidercontroller8002.util.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

@RequestMapping("/provider")
@RestController
public class providerController {
    @Autowired
    private PaymentFeign paymentFeign;


    @GetMapping("/id")
    public Object getFeign(){
        return paymentFeign.findStudent(1);
    }

    @Async
    @GetMapping("/ids")
    @TimeLimiter(name = "payment-service",fallbackMethod = "timeoutFallBack")
    public CompletableFuture<Object> getFeigns(){
        CompletableFuture<Object> completableFuture = CompletableFuture.supplyAsync((Supplier<Object>) () -> paymentFeign.findStudent(1));
        return completableFuture;
    }

    @Async
    @GetMapping("/retry")
    @Retry(name = "payment-service")
    public CompletableFuture<Object> getFeignRetry(){
        CompletableFuture<Object> completableFuture = CompletableFuture.supplyAsync((Supplier<Object>) () -> paymentFeign.findStudent(1));
        return completableFuture;
    }

    @GetMapping("/CircuitBreaker")
    @CircuitBreaker(name = "backendA")
    public Object getFeignBackendA(){
        return paymentFeign.findStudent(1);
    }

    public CompletableFuture<Object> timeoutFallBack(Throwable e){
        return CompletableFuture.completedFuture(BaseResult.ok("系统忙碌"));
    }


}
